package com.flurry.android;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.flurry.android.caching.CachingManager;
import com.flurry.android.caching.ObjectData;
import com.flurry.android.caching.ObjectOperationData;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;
import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

public class CacheSyncManager
{
  private static HashMap<String, AppCloudResponseHandler> i = new HashMap();
  private az f = null;
  private at g = null;
  private dc h = null;

  final void a()
  {
    this.g = ((at)FlurryAppCloud.a(gn.iI));
    this.f = ((az)FlurryAppCloud.a(gn.iJ));
    this.h = ((dc)FlurryAppCloud.a(gn.iK));
  }

  public boolean checkIfDeleted(String paramString)
  {
    return (this.h.contains(paramString)) && (this.h.r(paramString) == 2);
  }

  public at getPendingData()
  {
    return this.g;
  }

  public az getServerData()
  {
    return this.f;
  }

  public boolean hasRegisteredObjects()
  {
    return !this.h.isEmpty();
  }

  public void registerDirtyObject(String paramString, AppCloudResponseHandler paramAppCloudResponseHandler, int paramInt)
  {
    switch (paramInt)
    {
    case 3:
    default:
      Log.e("CacheSyncManager", "Register Object - STATE_UP_TO_DATE or default");
    case 1:
    case 2:
    case 4:
    }
    while (true)
    {
      if (paramAppCloudResponseHandler != null)
        i.put(paramString, paramAppCloudResponseHandler);
      dc localdc = this.h;
      Log.d("CacheDirtyOperationsDB", "--- Insert in " + localdc.cE + " : ---");
      if (localdc.contains(paramString))
        localdc.s(paramString);
      SQLiteDatabase localSQLiteDatabase = localdc.cD.getWritableDatabase();
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("objectsId", paramString);
      localContentValues.put("timestamp", Long.valueOf(System.currentTimeMillis()));
      localContentValues.put("value", Integer.valueOf(paramInt));
      localSQLiteDatabase.insert(localdc.cE, null, localContentValues);
      synchronize();
      return;
      if (this.h.contains(paramString))
      {
        int j = this.h.r(paramString);
        if (j == 2)
          try
          {
            paramAppCloudResponseHandler.handleResponse(new AppCloudResponse(new JSONObject("{code:\"400\",note:\"Object is already deleted, new one has to be created.\"}")));
            return;
          }
          catch (JSONException localJSONException)
          {
            localJSONException.printStackTrace();
            return;
          }
          catch (Exception localException)
          {
            localException.printStackTrace();
            return;
          }
        if (j == 1)
          paramInt = 1;
      }
    }
  }

  public void synchronize()
  {
    if (this.h == null);
    while (this.h.isEmpty())
      return;
    dc localdc = this.h;
    HashMap localHashMap = new HashMap();
    Cursor localCursor = localdc.cD.getReadableDatabase().query(localdc.cE, null, null, null, null, null, null);
    if (localCursor.moveToFirst())
    {
      int m = localCursor.getColumnIndex("objectsId");
      int n = localCursor.getColumnIndex("value");
      do
        localHashMap.put(localCursor.getString(m), Integer.valueOf(localCursor.getInt(n)));
      while (localCursor.moveToNext());
    }
    localCursor.close();
    Iterator localIterator = localHashMap.entrySet().iterator();
    label131: String str1;
    ObjectData localObjectData2;
    while (localIterator.hasNext())
    {
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      str1 = (String)localEntry.getKey();
      switch (((Integer)localEntry.getValue()).intValue())
      {
      case 3:
      default:
        Log.e("CacheSyncManager", "ERROR - STATE_UP_TO_DATE or default");
        break;
      case 1:
        boolean bool2 = this.g.isUserObject(str1);
        ObjectData localObjectData4 = this.g.getObjectById(str1);
        if ((bool2) || (localObjectData4 == null))
          Log.i("CacheSyncManager", "create user");
        for (ObjectOperationData localObjectOperationData3 = new ObjectOperationData(null); localObjectData4 != null; localObjectOperationData3 = new ObjectOperationData(null, localObjectData4.getCollectionName()))
        {
          Vector localVector2 = localObjectData4.getKeyValues();
          for (int k = 0; k < localVector2.size(); k++)
            localObjectOperationData3.addKeyValue(((NameValuePair)localVector2.get(k)).getName(), ((NameValuePair)localVector2.get(k)).getValue());
          Log.i("CacheSyncManager", "create object");
        }
        FlurryAppCloud.C().saveOrCreateOperation(localObjectOperationData3, new bc(this, str1, bool2));
        break;
      case 4:
        boolean bool1 = this.g.isUserObject(str1);
        ObjectData localObjectData3 = this.g.getObjectById(str1);
        String str2 = localObjectData3.getId();
        if (bool1);
        Vector localVector1;
        for (ObjectOperationData localObjectOperationData2 = new ObjectOperationData(str2); ; localObjectOperationData2 = new ObjectOperationData(str2, localObjectData3.getCollectionName()))
        {
          localVector1 = localObjectData3.getKeyValues();
          for (int j = 0; j < localVector1.size(); j++)
            localObjectOperationData2.addKeyValue(((NameValuePair)localVector1.get(j)).getName(), ((NameValuePair)localVector1.get(j)).getValue());
        }
        FlurryAppCloud.C().saveOrCreateOperation(localObjectOperationData2, new be(this, str2, localVector1));
        break;
      case 2:
        Log.i("CacheSyncManager", "STATE_DELETED");
        ObjectData localObjectData1 = this.g.getObjectById(str1);
        if (localObjectData1 == null)
        {
          localObjectData1 = this.f.getObjectById(str1);
          if (localObjectData1 == null)
            Log.e("CacheSyncManager", "ERROR! on delete.., no data for ID");
        }
        localObjectData2 = localObjectData1;
        if ((localObjectData2 == null) || (!localObjectData2.isObject()))
          break label639;
      }
    }
    label639: for (ObjectOperationData localObjectOperationData1 = new ObjectOperationData(str1, localObjectData2.getCollectionName()); ; localObjectOperationData1 = new ObjectOperationData(str1))
    {
      FlurryAppCloud.C().deleteOperation(localObjectOperationData1, new bg(this, str1));
      break label131;
      break;
    }
  }

  public void toStringAndPrint()
  {
    Log.i("CacheSyncManager", "============PENDING DATA==================");
    this.g.aI();
    Log.i("CacheSyncManager", "============SERVER DATA==================");
    this.f.aI();
    Log.i("CacheSyncManager", "============Registered Dirty Objects==================");
    this.h.readAll();
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.flurry.android.CacheSyncManager
 * JD-Core Version:    0.6.2
 */